<?php

namespace app\services;

use app\traits\Page;
use plugin\admin\app\model\Question;

class QuestionService
{
    public static function list($page, $length = 20, $categoryId = 0)
    {
        $offset = ($page - 1) * $length;

        $query = new Question();

        if ($categoryId > 0) {
            $query = $query->where('category_id', $categoryId);
        }

        $totalCount = (clone $query)->count();
        $list = $query->offset($offset)->limit($length)->orderBy('sort')->get()->toArray();

        return Page::wrap($totalCount, $page, $length, $list);
    }
}